﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.GameContent.UI.Elements;
using Terraria.Localization;
using Terraria.UI;
using Terraria.UI.Gamepad;

namespace Terraria.ModLoader.UI
{
	// Token: 0x020002B5 RID: 693
	internal class UIModSources : UIState
	{
		// Token: 0x06001B56 RID: 6998 RVA: 0x004306F0 File Offset: 0x0042E8F0
		public override void OnInitialize()
		{
			this.uIElement = new UIElement();
			this.uIElement.Width.Set(0f, 0.8f);
			this.uIElement.MaxWidth.Set(600f, 0f);
			this.uIElement.Top.Set(220f, 0f);
			this.uIElement.Height.Set(-220f, 1f);
			this.uIElement.HAlign = 0.5f;
			this.uIPanel = new UIPanel();
			this.uIPanel.Width.Set(0f, 1f);
			this.uIPanel.Height.Set(-110f, 1f);
			this.uIPanel.BackgroundColor = new Color(33, 43, 79) * 0.8f;
			this.uIElement.Append(this.uIPanel);
			this.uiLoader = new UILoaderAnimatedImage(0.5f, 0.5f, 1f);
			this.modList = new UIList();
			this.modList.Width.Set(-25f, 1f);
			this.modList.Height.Set(0f, 1f);
			this.modList.ListPadding = 5f;
			this.uIPanel.Append(this.modList);
			UIScrollbar uiscrollbar = new UIScrollbar();
			uiscrollbar.SetView(100f, 1000f);
			uiscrollbar.Height.Set(0f, 1f);
			uiscrollbar.HAlign = 1f;
			this.uIPanel.Append(uiscrollbar);
			this.modList.SetScrollbar(uiscrollbar);
			UITextPanel<string> uitextPanel = new UITextPanel<string>(Language.GetTextValue("tModLoader.MenuModSources"), 0.8f, true);
			uitextPanel.HAlign = 0.5f;
			uitextPanel.Top.Set(-35f, 0f);
			uitextPanel.SetPadding(15f);
			uitextPanel.BackgroundColor = new Color(73, 94, 171);
			this.uIElement.Append(uitextPanel);
			UITextPanel<string> uitextPanel2 = new UITextPanel<string>(Language.GetTextValue("tModLoader.MSBuildAll"), 1f, false);
			uitextPanel2.Width.Set(-10f, 0.5f);
			uitextPanel2.Height.Set(25f, 0f);
			uitextPanel2.VAlign = 1f;
			uitextPanel2.Top.Set(-65f, 0f);
			uitextPanel2.OnMouseOver += UICommon.FadedMouseOver;
			uitextPanel2.OnMouseOut += UICommon.FadedMouseOut;
			uitextPanel2.OnClick += this.BuildMods;
			this.uIElement.Append(uitextPanel2);
			UITextPanel<string> uitextPanel3 = new UITextPanel<string>(Language.GetTextValue("tModLoader.MSBuildReloadAll"), 1f, false);
			uitextPanel3.CopyStyle(uitextPanel2);
			uitextPanel3.HAlign = 1f;
			uitextPanel3.OnMouseOver += UICommon.FadedMouseOver;
			uitextPanel3.OnMouseOut += UICommon.FadedMouseOut;
			uitextPanel3.OnClick += this.BuildAndReload;
			this.uIElement.Append(uitextPanel3);
			UITextPanel<string> uitextPanel4 = new UITextPanel<string>(Language.GetTextValue("UI.Back"), 1f, false);
			uitextPanel4.CopyStyle(uitextPanel2);
			uitextPanel4.Width.Set(-10f, 0.333333343f);
			uitextPanel4.Top.Set(-20f, 0f);
			uitextPanel4.OnMouseOver += UICommon.FadedMouseOver;
			uitextPanel4.OnMouseOut += UICommon.FadedMouseOut;
			uitextPanel4.OnClick += this.BackClick;
			this.uIElement.Append(uitextPanel4);
			UITextPanel<string> uitextPanel5 = new UITextPanel<string>(Language.GetTextValue("tModLoader.MSOpenSources"), 1f, false);
			uitextPanel5.CopyStyle(uitextPanel4);
			uitextPanel5.HAlign = 0.5f;
			uitextPanel5.OnMouseOver += UICommon.FadedMouseOver;
			uitextPanel5.OnMouseOut += UICommon.FadedMouseOut;
			uitextPanel5.OnClick += this.OpenSources;
			this.uIElement.Append(uitextPanel5);
			UITextPanel<string> uitextPanel6 = new UITextPanel<string>(Language.GetTextValue("tModLoader.MSManagePublished"), 1f, false);
			uitextPanel6.CopyStyle(uitextPanel4);
			uitextPanel6.HAlign = 1f;
			uitextPanel6.OnMouseOver += UICommon.FadedMouseOver;
			uitextPanel6.OnMouseOut += UICommon.FadedMouseOut;
			uitextPanel6.OnClick += this.ManagePublished;
			this.uIElement.Append(uitextPanel6);
			base.Append(this.uIElement);
		}

		// Token: 0x06001B57 RID: 6999 RVA: 0x0001399F File Offset: 0x00011B9F
		private void ManagePublished(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(11, -1, -1, 1, 1f, 0f);
			Main.menuMode = 10011;
		}

		// Token: 0x06001B58 RID: 7000 RVA: 0x00013639 File Offset: 0x00011839
		private void BackClick(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(11, -1, -1, 1, 1f, 0f);
			Main.menuMode = 0;
		}

		// Token: 0x06001B59 RID: 7001 RVA: 0x000139C0 File Offset: 0x00011BC0
		private void OpenSources(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(10, -1, -1, 1, 1f, 0f);
			Directory.CreateDirectory(ModLoader.ModSourcePath);
			Process.Start(ModLoader.ModSourcePath);
		}

		// Token: 0x06001B5A RID: 7002 RVA: 0x000139ED File Offset: 0x00011BED
		private void BuildMods(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(10, -1, -1, 1, 1f, 0f);
			if (this.modList.Count > 0)
			{
				ModLoader.reloadAfterBuild = false;
				ModLoader.buildAll = true;
				Main.menuMode = 10004;
			}
		}

		// Token: 0x06001B5B RID: 7003 RVA: 0x00013A28 File Offset: 0x00011C28
		private void BuildAndReload(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(10, -1, -1, 1, 1f, 0f);
			if (this.modList.Count > 0)
			{
				ModLoader.reloadAfterBuild = true;
				ModLoader.buildAll = true;
				Main.menuMode = 10004;
			}
		}

		// Token: 0x06001B5C RID: 7004 RVA: 0x00013A63 File Offset: 0x00011C63
		public override void Draw(SpriteBatch spriteBatch)
		{
			base.Draw(spriteBatch);
			UILinkPointNavigator.Shortcuts.BackButtonCommand = 1;
		}

		// Token: 0x06001B5D RID: 7005 RVA: 0x00013A72 File Offset: 0x00011C72
		public override void OnActivate()
		{
			this.uIPanel.Append(this.uiLoader);
			this.modList.Clear();
			this.items.Clear();
			this.Populate();
		}

		// Token: 0x06001B5E RID: 7006 RVA: 0x00430BA8 File Offset: 0x0042EDA8
		internal void Populate()
		{
			if (SynchronizationContext.Current == null)
			{
				SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
			}
			Task.Factory.StartNew<Tuple<string[], LocalMod[]>>(delegate
			{
				string[] item = ModLoader.FindModSources();
				LocalMod[] item2 = ModLoader.FindMods();
				return Tuple.Create<string[], LocalMod[]>(item, item2);
			}).ContinueWith(delegate(Task<Tuple<string[], LocalMod[]>> task)
			{
				string[] item = task.Result.Item1;
				LocalMod[] item2 = task.Result.Item2;
				string[] array = item;
				for (int i = 0; i < array.Length; i++)
				{
					string sourcePath = array[i];
					LocalMod builtMod = item2.SingleOrDefault((LocalMod m) => m.Name == Path.GetFileName(sourcePath));
					this.items.Add(new UIModSourceItem(sourcePath, builtMod));
				}
				this.updateNeeded = true;
			}, TaskScheduler.FromCurrentSynchronizationContext());
		}

		// Token: 0x06001B5F RID: 7007 RVA: 0x00430C08 File Offset: 0x0042EE08
		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime);
			if (!this.updateNeeded)
			{
				return;
			}
			this.updateNeeded = false;
			this.uIPanel.RemoveChild(this.uiLoader);
			this.modList.Clear();
			this.modList.AddRange(this.items);
		}

		// Token: 0x0400187D RID: 6269
		private UIList modList;

		// Token: 0x0400187E RID: 6270
		private readonly List<UIModSourceItem> items = new List<UIModSourceItem>();

		// Token: 0x0400187F RID: 6271
		private bool updateNeeded;

		// Token: 0x04001880 RID: 6272
		private UIElement uIElement;

		// Token: 0x04001881 RID: 6273
		private UIPanel uIPanel;

		// Token: 0x04001882 RID: 6274
		private UILoaderAnimatedImage uiLoader;
	}
}
